package com.hs.srm.admin.modules.cutpay.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sunnykid.character.UtilCharacter;
import sunnykid.lang.LangException;
import sunnykid.lang.SunnykidDate;
import sunnykid.text.SunnykidText;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.cutpay.dao.CutpayHeaderDao;
import com.hs.srm.admin.modules.cutpay.dao.CutpayItemDao;
import com.hs.srm.admin.modules.cutpay.entity.CutpayHeaderEntity;
import com.hs.srm.admin.modules.cutpay.entity.CutpayItemEntity;
import com.hs.srm.admin.modules.cutpay.service.CutpayHeaderService;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;

/**
 * 扣款单头信息Service控制层实现类
 * <p>標題：</p>
 * <p>描述：</p>
 * <p>版權：版權所有 (c) 2019</p>
 * <p>組織：厚声集团</p>
 *
 * @author 鐘曉籟
 * @version V1.0.0
 * @date 2019年4月2日 上午9:46:29
 * @since V1.0.0
 */
@Service("cutpayHeaderService")
public class CutpayHeaderServiceImpl extends ServiceImpl<CutpayHeaderDao, CutpayHeaderEntity> implements
		CutpayHeaderService {

	@Autowired
	private CutpayHeaderDao cutpayHeaderDao;

	@Autowired
	private CutpayItemDao cutpayItemDao;

	@Override
	public PageUtils selectDetailPage(Map<String, Object> params) {
		Page<CutpayHeaderEntity> page = this.selectDetailPage(new QueryParam<CutpayHeaderEntity>(params).getPage(),
				new EntityWrapper<CutpayHeaderEntity>(), params);
		return new PageUtils(page);
	}

	private Page<CutpayHeaderEntity> selectDetailPage(Page<CutpayHeaderEntity> page,
			Wrapper<CutpayHeaderEntity> wrapper, Map<String, Object> params) {
		page.setRecords(this.cutpayHeaderDao.selectDetailPage(page, wrapper, params));
		return page;
	}

	@Override
	public Map<String, Object> detailDataForAddPart1(String poNo) {
		return this.cutpayHeaderDao.detailDataForAddPart1(poNo);
	}

	@Override
	public List<Map<String, Object>> detailDataForAddPart2(String poNo) {
		return this.cutpayHeaderDao.detailDataForAddPart2(poNo);
	}

	@Override
	public void saveData(JSONObject jsonObj, String currentUser) throws LangException {
		String poNo = jsonObj.getString("poNo");
		Map<String, Object> head = this.cutpayHeaderDao.detailDataForAddPart1(poNo);
		CutpayHeaderEntity headerEntity = new CutpayHeaderEntity();
		Date currentDate = new Date();

		String coNo = SunnykidText.getUUID();

		headerEntity.setPkId(SunnykidText.getUUID());
		headerEntity.setCoNo(coNo);
		headerEntity.setCoType((String)jsonObj.get("coType"));
		headerEntity.setPoNo(poNo);
		headerEntity.setFactory((String)jsonObj.get("companyCode"));
		headerEntity.setCreationDate(currentDate);
		headerEntity.setCreatedBy(currentUser);
		headerEntity.setPurchGroup((String)head.get("purchGroup"));
		headerEntity.setVendorCode((String)head.get("vendorCode"));
		headerEntity.setVendorName((String)head.get("vendorName"));
		this.insert(headerEntity);

		List<Map<String, Object>> arr = (List<Map<String, Object>>)jsonObj.get("items");

		// Map<String, Object> m = arr.get(0);
		// System.out.println("================");
		// for(Entry<String, Object> e : m.entrySet()) {
		// System.out.println(e.getKey() + " : " + e.getKey().getClass().getName() + " ,,, " + e.getValue() + " : " +
		// e.getValue().getClass().getName());
		// }
		// System.out.println("================");

		// itemNo:20,cutMoney:40,complainDate:2019-05-15,remark:4444
		List<Map<String, Object>> mapList = this.detailDataForAddPart2(poNo);
		for(Map<String, Object> map : mapList) {
			String itemNo = (String)map.get("itemNo");
			if(itemNo != null) {
				for(Map<String, Object> paramsMap : arr) {
					String paramItemNo = (String)paramsMap.get("itemNo");
					String cutMoney = (String)paramsMap.get("cutMoney");
					String complainDate = (String)paramsMap.get("complainDate");
					String remark = (String)paramsMap.get("remark");
					if(itemNo.equals(paramItemNo) && !SunnykidText.isEmpty(cutMoney) && !cutMoney.equals("0")) {
						BigDecimal cutMoneyVal = new BigDecimal(cutMoney);
						BigDecimal sumMoneyVal = (BigDecimal)map.get("sumMoney");
						BigDecimal payMoney = sumMoneyVal.subtract(cutMoneyVal);
						CutpayItemEntity item = new CutpayItemEntity();
						item.setCoItemNo(SunnykidText.getUUID());
						item.setComplainDate((complainDate == null || complainDate.trim().equals(UtilCharacter.EMPTY) || complainDate
								.equals("null")) ? null : SunnykidDate.parseSunnykidDate(complainDate));
						item.setCoNo(coNo);
						item.setCreatedBy(currentUser);
						item.setCreationDate(currentDate);
						item.setCurrencyUnit((String)map.get("currencyCode"));//
						item.setCutMoney(cutMoneyVal);
						Date dateFp = (Date)map.get("dateFp");
						item.setInvoiceDate(dateFp);// dateFp
						item.setInvoiceMoney(sumMoneyVal);
						item.setInvoiceNo((String)map.get("invoiceNo"));
						item.setMaterialCode((String)map.get("materialCode"));
						item.setMaterialDesc((String)map.get("materialDesc"));
						item.setPayMoney(payMoney);
						item.setPoItemNo(itemNo);
						item.setPoNo(poNo);
						item.setRemark(remark);
						this.cutpayItemDao.insert(item);
						continue;
					}
				}
			}
		}
	}

}
